package com.ncmmall.application.business.merchant.freight;

import com.ncmmall.domain.business.merchant.freight.MerchantFreight;
import com.ncmmall.domain.business.merchant.freight.MerchantFreightRepository;
import com.ncmmall.domain.business.merchant.freight.MerchantFreightTemplate;
import com.ncmmall.domain.business.merchant.freight.MerchantFreightTemplateRepository;
import com.ncmmall.domain.business.merchant.freight.QMerchantFreight;
import com.ncmmall.domain.business.merchant.freight.QMerchantFreightTemplate;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.domain.security.user.WebUserRepository;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * 商家运费设置
 *
 * @author likaikai
 * @date 2019-07-08 11:44
 */
@Service
@Transactional
public class MerchantFreightApplication {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private MerchantFreightRepository merchantFreightRepository;

    @Autowired
    private MerchantFreightTemplateRepository templateRepository;

    @Autowired
    private WebUserRepository webUserRepository;

    /**
     * 添加运费模板
     *
     * @param merchant
     * @param template
     */
    public void create(WebUser merchant, MerchantFreightTemplate template) {

        template.setMerchant(merchant);
        template.setStatus(2);
        template.setCreateTime(new Timestamp(System.currentTimeMillis()));
        templateRepository.save(template);

        if (template.getMerchantFreights() != null && !template.getMerchantFreights().isEmpty()) {

            List<MerchantFreight> freights = Lists.newArrayList();


            for (MerchantFreight freight : template.getMerchantFreights()) {

                if (Strings.isNullOrEmpty(freight.getCitys())) {
                    continue;
                }

                freight.setTemplate(template);
                freight.setMerchant(merchant);
                freights.add(freight);
            }

            merchantFreightRepository.save(freights);

            for (MerchantFreight merchantFreight : freights) {
                List<String> citys = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(merchantFreight.getCitys());
                for (String city : citys) {
                    MerchantFreight freight = new MerchantFreight();
                    freight.setParent(merchantFreight);
                    freight.setCity(city);
                    freight.setMerchant(merchant);
                    freight.setTemplate(template);

                    if (template.getType() == 1) {
                        freight.setFirstWeight(merchantFreight.getFirstWeight());
                        freight.setFirstWeightAmount(merchantFreight.getFirstWeightAmount());
                        freight.setOtherWeight(merchantFreight.getOtherWeight());
                        freight.setOtherWeightAmount(merchantFreight.getOtherWeightAmount());
                    }

                    if (template.getType() == 2) {
                        freight.setFirstPiece(merchantFreight.getFirstPiece());
                        freight.setFirstPieceAmount(merchantFreight.getFirstPieceAmount());
                        freight.setOtherPiece(merchantFreight.getOtherPiece());
                        freight.setOtherPieceAmount(merchantFreight.getOtherPieceAmount());
                    }

                    merchantFreightRepository.save(freight);
                }
            }
        }

    }

    /**
     * 修改运费模板
     *
     * @param merchant
     * @param params
     */
    public void modify(WebUser merchant, MerchantFreightTemplate params) {

        MerchantFreightTemplate template = queryChannelService.findOne(QMerchantFreightTemplate.merchantFreightTemplate.id.eq(params.getId()), MerchantFreightTemplate.class);
        template.setDefaultWeight(params.getDefaultWeight());
        template.setDefaultWeightAmount(params.getDefaultWeightAmount());
        template.setDefaultAddWeight(params.getDefaultAddWeight());
        template.setDefaultAddWeightAmount(params.getDefaultAddWeightAmount());

        template.setDefaultPiece(params.getDefaultPiece());
        template.setDefaultPieceAmount(params.getDefaultPieceAmount());
        template.setDefaultAddPiece(params.getDefaultAddPiece());
        template.setDefaultAddPieceAmount(params.getDefaultAddPieceAmount());

        template.setName(params.getName());
        template.setType(params.getType());
        templateRepository.save(template);

        List<MerchantFreight> oldFreights = queryChannelService.findAll(QMerchantFreight.merchantFreight.parent.isNotNull().and(QMerchantFreight.merchantFreight.template.id.eq(params.getId())), MerchantFreight.class);
        merchantFreightRepository.delete(oldFreights);

        List<MerchantFreight> oldTopFreights = queryChannelService.findAll(QMerchantFreight.merchantFreight.template.id.eq(params.getId()), MerchantFreight.class);
        merchantFreightRepository.delete(oldTopFreights);

        List<MerchantFreight> freights = Lists.newArrayList();
        for (MerchantFreight freight : params.getMerchantFreights()) {

            if (Strings.isNullOrEmpty(freight.getCitys())) {
                continue;
            }

            freight.setTemplate(template);
            freight.setMerchant(merchant);
            freights.add(freight);
        }

        merchantFreightRepository.save(freights);

        for (MerchantFreight merchantFreight : freights) {
            List<String> citys = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(merchantFreight.getCitys());
            for (String city : citys) {
                MerchantFreight freight = new MerchantFreight();
                freight.setParent(merchantFreight);
                freight.setCity(city);
                freight.setMerchant(merchant);
                freight.setTemplate(template);

                if (template.getType() == 1) {
                    freight.setFirstWeight(merchantFreight.getFirstWeight());
                    freight.setFirstWeightAmount(merchantFreight.getFirstWeightAmount());
                    freight.setOtherWeight(merchantFreight.getOtherWeight());
                    freight.setOtherWeightAmount(merchantFreight.getOtherWeightAmount());
                }

                if (template.getType() == 2) {
                    freight.setFirstPiece(merchantFreight.getFirstPiece());
                    freight.setFirstPieceAmount(merchantFreight.getFirstPieceAmount());
                    freight.setOtherPiece(merchantFreight.getOtherPiece());
                    freight.setOtherPieceAmount(merchantFreight.getOtherPieceAmount());
                }

                merchantFreightRepository.save(freight);
            }
        }
    }

    /**
     * 禁用运费模板
     *
     * @param params
     */
    public void disable(MerchantFreightTemplate params) {
        MerchantFreightTemplate template = queryChannelService.findOne(QMerchantFreightTemplate.merchantFreightTemplate.id.eq(params.getId()), MerchantFreightTemplate.class);
        template.setStatus(2);
        templateRepository.save(template);

    }

    /**
     * 启用运费模板
     *
     * @param params
     */
    public void enabled(MerchantFreightTemplate params) {
        MerchantFreightTemplate template = queryChannelService.findOne(QMerchantFreightTemplate.merchantFreightTemplate.id.eq(params.getId()), MerchantFreightTemplate.class);
        template.setStatus(1);
        templateRepository.save(template);
    }
}
